//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using DotNetNuke.Application;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Host;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Entities.Modules.Communications;
using DotNetNuke.Framework;
using DotNetNuke.Security.Permissions;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.ControlPanels;
using DotNetNuke.UI.Skins.EventListeners;
namespace DotNetNuke
{
	[Obsolete("This class is obsolete.  Please use DotNetNuke.UI.Skins.Skin.")]
	public class Skin : DotNetNuke.UI.Skins.Skin
	{
	}
}
namespace DotNetNuke.UI.Skins
{
	public class Skin : Framework.UserControlBase
	{
		private ArrayList _actionEventListeners;
		private ModuleCommunicate _Communicator = new ModuleCommunicate();
		private Control _ControlPanel;
		private Dictionary<string, Pane> _panes;
		private string _skinSrc;
		public string PANE_LOAD_ERROR = Localization.GetString("PaneNotFound.Error");
		public string CONTRACTEXPIRED_ERROR = Localization.GetString("ContractExpired.Error");
		public string TABACCESS_ERROR = Localization.GetString("TabAccess.Error");
		public string MODULEACCESS_ERROR = Localization.GetString("ModuleAccess.Error");
		public string CRITICAL_ERROR = Localization.GetString("CriticalError.Error");
		public string MODULELOAD_WARNING = Localization.GetString("ModuleLoadWarning.Error");
		public string MODULELOAD_WARNINGTEXT = Localization.GetString("ModuleLoadWarning.Text");
		public static string MODULELOAD_ERROR = Localization.GetString("ModuleLoad.Error");
		public static string CONTAINERLOAD_ERROR = Localization.GetString("ContainerLoad.Error");
		public static string MODULEADD_ERROR = Localization.GetString("ModuleAdd.Error");
		internal Control ControlPanel {
			get {
				if (_ControlPanel == null) {
					_ControlPanel = FindControl("ControlPanel");
				}
				return _ControlPanel;
			}
		}
		internal ModuleCommunicate Communicator {
			get { return _Communicator; }
		}
		internal Dictionary<string, Pane> Panes {
			get {
				if (_panes == null) {
					_panes = new Dictionary<string, Pane>();
				}
				return _panes;
			}
		}
		public ArrayList ActionEventListeners {
			get {
				if (_actionEventListeners == null) {
					_actionEventListeners = new ArrayList();
				}
				return _actionEventListeners;
			}
			set { _actionEventListeners= value; }
		}
		public string SkinPath {
			get { return this.TemplateSourceDirectory + "/"; }
		}
		public string SkinSrc {
			get { return _skinSrc; }
			set { _skinSrc = value; }
		}
		private bool CheckExpired()
		{
			bool blnExpired = false;
			if (PortalSettings.ExpiryDate != Null.NullDate) {
				if (Convert.ToDateTime(PortalSettings.ExpiryDate) < DateTime.Now && PortalSettings.ActiveTab.ParentId != PortalSettings.SuperTabId) {
					blnExpired = true;
				}
			}
			return blnExpired;
		}
		private void InjectControlPanel()
		{
			if (Request.QueryString["dnnprintmode"] != "true") {
				ControlPanelBase objControlPanel = ControlUtilities.LoadControl<ControlPanelBase>(this, Host.ControlPanel);
				if (ControlPanel == null) {
					HtmlForm objForm = (HtmlForm)Parent.FindControl("Form");
					if (objForm != null) {
						objForm.Controls.AddAt(0, objControlPanel);
					} else {
						Page.Controls.AddAt(0, objControlPanel);
					}
				} else {
					ControlPanel.Controls.Add(objControlPanel);
				}
			}
		}

		private void LoadPanes()
		{
			HtmlContainerControl objPaneControl;
			foreach (Control ctlControl in Controls) {
				if (ctlControl is HtmlContainerControl)
				{
					objPaneControl = ctlControl as HtmlContainerControl;
					if (objPaneControl != null && !string.IsNullOrEmpty(objPaneControl.ID))
					{
						switch (objPaneControl.TagName.ToLowerInvariant())
						{
							case "td":
							case "div":
							case "span":
							case "p":
								if (objPaneControl.ID.ToLower() != "controlpanel")
								{
									PortalSettings.ActiveTab.Panes.Add(objPaneControl.ID);
									Panes.Add(objPaneControl.ID.ToLowerInvariant(), new Pane(objPaneControl));
								}
								else
								{
									_ControlPanel = objPaneControl;
								}
								break;
						}
					}
				}
			}
		}
		private bool ProcessMasterModules()
		{
			bool bSuccess = true;
			if (TabPermissionController.CanViewPage()) {
				if (!CheckExpired()) {
					if ((PortalSettings.ActiveTab.StartDate < DateTime.Now && PortalSettings.ActiveTab.EndDate > DateTime.Now)
						 || TabPermissionController.CanAdminPage() || Common.Globals.IsLayoutMode())
					{
						if (PortalSettings.ActiveTab.Modules.Count > 0) {
							foreach (ModuleInfo objModule in PortalSettings.ActiveTab.Modules) {
								if (ModulePermissionController.CanViewModule(objModule) && objModule.IsDeleted == false) {
									if ((objModule.StartDate < DateTime.Now && objModule.EndDate > DateTime.Now)
										|| Common.Globals.IsLayoutMode() || Common.Globals.IsEditMode())
									{
										Pane pane = null;
										bool bFound = Panes.TryGetValue(objModule.PaneName.ToLowerInvariant(), out pane);
										if (!bFound) {
											bFound = Panes.TryGetValue(Common.Globals.glbDefaultPane.ToLowerInvariant(), out pane);
										}
										if (bFound) {
											bSuccess = InjectModule(pane, objModule);
										} else {
											ModuleLoadException lex;
											lex = new ModuleLoadException(PANE_LOAD_ERROR);
											Controls.Add(new ErrorContainer(PortalSettings, MODULELOAD_ERROR, lex).Container);
											DotNetNuke.Services.Exceptions.Exceptions.LogException(lex);
										}
									}
								}
							}
						}
					} else {
						AddPageMessage(this, "", TABACCESS_ERROR, UI.Skins.Controls.ModuleMessage.ModuleMessageType.YellowWarning);
					}
				} else {
					AddPageMessage(this, "", string.Format(CONTRACTEXPIRED_ERROR, PortalSettings.PortalName, Common.Globals.GetMediumDate(PortalSettings.ExpiryDate.ToString()), PortalSettings.Email), UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
				}
			} else {
				Response.Redirect(Common.Globals.AccessDeniedURL(TABACCESS_ERROR), true);
			}
			return bSuccess;
		}
		private void ProcessPanes()
		{
			foreach (KeyValuePair<string, Pane> kvp in Panes) {
				kvp.Value.ProcessPane();
			}
		}
		private bool ProcessSlaveModule()
		{
			ModuleController objModules = new ModuleController();
			ModuleInfo objModule = null;
			ModuleInfo slaveModule = null;
			int moduleId = -1;
			string key = "";
			bool bSuccess = true;
			if (Request.QueryString["mid"] != null) {
				Int32.TryParse(Request.QueryString["mid"], out moduleId);
			}
			if (Request.QueryString["ctl"] != null) {
				key = Request.QueryString["ctl"];
			}
			if (Request.QueryString["moduleid"] != null && (key.ToLower() == "module" || key.ToLower() == "help")) {
				Int32.TryParse(Request.QueryString["moduleid"], out moduleId);
			}
			if (moduleId != -1) {
				objModule = objModules.GetModule(moduleId, PortalSettings.ActiveTab.TabID, false);
				if (objModule != null) {
					slaveModule = objModule.Clone();
				}
			}
			if (slaveModule == null) {
				slaveModule = new ModuleInfo();
				slaveModule.ModuleID = moduleId;
				slaveModule.ModuleDefID = -1;
				slaveModule.TabID = PortalSettings.ActiveTab.TabID;
			}
			if (Request.QueryString["moduleid"] != null && (key.ToLower() == "module" || key.ToLower() == "help")) {
				slaveModule.ModuleDefID = -1;
			}
			if (Request.QueryString["dnnprintmode"] != "true") {
				slaveModule.ModuleTitle = "";
			}
			slaveModule.Header = "";
			slaveModule.Footer = "";
			slaveModule.StartDate = DateTime.MinValue;
			slaveModule.EndDate = DateTime.MaxValue;
			slaveModule.PaneName = Common.Globals.glbDefaultPane;
			slaveModule.Visibility = VisibilityState.None;
			slaveModule.Color = "";
			slaveModule.Border = "";
			slaveModule.DisplayTitle = true;
			slaveModule.DisplayPrint = false;
			slaveModule.DisplaySyndicate = false;
			slaveModule.ContainerSrc = PortalSettings.ActiveTab.ContainerSrc;
			if (string.IsNullOrEmpty(slaveModule.ContainerSrc)) {
				slaveModule.ContainerSrc = PortalSettings.DefaultPortalContainer;
			}
			slaveModule.ContainerSrc = SkinController.FormatSkinSrc(slaveModule.ContainerSrc, PortalSettings);
			slaveModule.ContainerPath = SkinController.FormatSkinPath(slaveModule.ContainerSrc);
			Pane pane = null;
			bool bFound = Panes.TryGetValue(Common.Globals.glbDefaultPane.ToLowerInvariant(), out pane);
			ModuleControlInfo objModuleControl = ModuleControlController.GetModuleControlByControlKey(key, slaveModule.ModuleDefID);
			if (objModuleControl != null) {
				slaveModule.ModuleControlId = objModuleControl.ModuleControlID;
				slaveModule.IconFile = objModuleControl.IconFile;
				if (ModulePermissionController.HasModuleAccess(slaveModule.ModuleControl.ControlType, Null.NullString, slaveModule)) {
					bSuccess = InjectModule(pane, slaveModule);
				} else {
					Response.Redirect(Common.Globals.AccessDeniedURL(MODULEACCESS_ERROR), true);
				}
			}
			return bSuccess;
		}
		protected override void OnInit(System.EventArgs e)
		{
			base.OnInit(e);
			bool bSuccess = true;
			LoadPanes();
			if (!Common.Globals.IsAdminControl())
			{
				bSuccess = ProcessMasterModules();
			} else {
				bSuccess = ProcessSlaveModule();
			}
			InjectControlPanel();
			ProcessPanes();
			if (Request.QueryString["error"] != null) {
				AddPageMessage(this, CRITICAL_ERROR, Server.HtmlEncode(Request.QueryString["error"]), UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
			}
			if (!TabPermissionController.CanAdminPage()) {
				if (!bSuccess) {
					AddPageMessage(this, MODULELOAD_WARNING, string.Format(MODULELOAD_WARNINGTEXT, PortalSettings.Email), UI.Skins.Controls.ModuleMessage.ModuleMessageType.YellowWarning);
				}
			}
			foreach (SkinEventListener listener in DotNetNukeContext.Current.SkinEventListeners) {
				if (listener.EventType == SkinEventType.OnSkinInit) {
					listener.SkinEvent.Invoke(this, new SkinEventArgs(this));
				}
			}
		}
		protected override void OnLoad(System.EventArgs e)
		{
			base.OnLoad(e);
			foreach (SkinEventListener listener in DotNetNukeContext.Current.SkinEventListeners) {
				if (listener.EventType == SkinEventType.OnSkinLoad) {
					listener.SkinEvent.Invoke(this, new SkinEventArgs(this));
				}
			}
		}
		protected override void OnPreRender(System.EventArgs e)
		{
			base.OnPreRender(e);
			foreach (SkinEventListener listener in DotNetNukeContext.Current.SkinEventListeners) {
				if (listener.EventType == SkinEventType.OnSkinPreRender) {
					listener.SkinEvent.Invoke(this, new SkinEventArgs(this));
				}
			}
		}
		protected override void OnUnload(EventArgs e)
		{
			base.OnUnload(e);
			foreach (SkinEventListener listener in DotNetNukeContext.Current.SkinEventListeners) {
				if (listener.EventType == SkinEventType.OnSkinUnLoad) {
					listener.SkinEvent.Invoke(this, new SkinEventArgs(this));
				}
			}
		}
		public bool InjectModule(Pane objPane, ModuleInfo objModule)
		{
			bool bSuccess = true;
			try {
				objPane.InjectModule(objModule);
			} catch (Exception ex) {
				bSuccess = false;
			}
			return bSuccess;
		}
		public void RegisterModuleActionEvent(int ModuleID, ActionEventHandler e)
		{
			ActionEventListeners.Add(new ModuleActionEventListener(ModuleID, e));
		}
		private static DotNetNuke.UI.Skins.Skin LoadSkin(PageBase Page, string SkinPath)
		{
			DotNetNuke.UI.Skins.Skin ctlSkin = null;
			try {
				string SkinSrc = SkinPath;
				if (SkinPath.ToLower().IndexOf(Common.Globals.ApplicationPath) != -1) {
					SkinPath = SkinPath.Remove(0, Common.Globals.ApplicationPath.Length);
				}
				ctlSkin = ControlUtilities.LoadControl<DotNetNuke.UI.Skins.Skin>(Page, SkinPath);
				ctlSkin.SkinSrc = SkinSrc;
				ctlSkin.DataBind();
			} catch (Exception exc) {
				PageLoadException lex = new PageLoadException("Unhandled error loading page.", exc);
				if (TabPermissionController.CanAdminPage()) {
					Label SkinError = (Label)Page.FindControl("SkinError");
					SkinError.Text = string.Format(Localization.GetString("SkinLoadError", Localization.GlobalResourceFile), SkinPath, Page.Server.HtmlEncode(exc.Message));
					SkinError.Visible = true;
				}
				DotNetNuke.Services.Exceptions.Exceptions.LogException(lex);
			}
			return ctlSkin;
		}
		private static void AddModuleMessage(Control objControl, string Heading, string Message, UI.Skins.Controls.ModuleMessage.ModuleMessageType objModuleMessageType, string IconSrc)
		{
			if (objControl != null) {
				if (!String.IsNullOrEmpty(Message)) {
					PlaceHolder MessagePlaceHolder = (PlaceHolder)objControl.Parent.FindControl("MessagePlaceHolder");
					if (MessagePlaceHolder != null) {
						MessagePlaceHolder.Visible = true;
						UI.Skins.Controls.ModuleMessage objModuleMessage;
						objModuleMessage = GetModuleMessageControl(Heading, Message, objModuleMessageType, IconSrc);
						MessagePlaceHolder.Controls.Add(objModuleMessage);
					}
				}
			}
		}
		private static void AddPageMessage(Control objControl, string Heading, string Message, UI.Skins.Controls.ModuleMessage.ModuleMessageType objModuleMessageType, string IconSrc)
		{
			if (!String.IsNullOrEmpty(Message)) {
				Control ContentPane = (Control)objControl.FindControl(Common.Globals.glbDefaultPane);
				if (ContentPane != null) {
					UI.Skins.Controls.ModuleMessage objModuleMessage;
					objModuleMessage = GetModuleMessageControl(Heading, Message, objModuleMessageType, IconSrc);
					ContentPane.Controls.AddAt(0, objModuleMessage);
				}
			}
		}
		public static void AddModuleMessage(PortalModuleBase objControl, string Message, UI.Skins.Controls.ModuleMessage.ModuleMessageType objModuleMessageType)
		{
			AddModuleMessage(objControl, "", Message, objModuleMessageType, Null.NullString);
		}
		public static void AddModuleMessage(PortalModuleBase objControl, string Heading, string Message, UI.Skins.Controls.ModuleMessage.ModuleMessageType objModuleMessageType)
		{
			AddModuleMessage(objControl, Heading, Message, objModuleMessageType, Null.NullString);
		}
		public static void AddModuleMessage(Control objControl, string Message, UI.Skins.Controls.ModuleMessage.ModuleMessageType objModuleMessageType)
		{
			AddModuleMessage(objControl, "", Message, objModuleMessageType, Null.NullString);
		}
		public static void AddModuleMessage(Control objControl, string Heading, string Message, UI.Skins.Controls.ModuleMessage.ModuleMessageType objModuleMessageType)
		{
			AddModuleMessage(objControl, Heading, Message, objModuleMessageType, Null.NullString);
		}
		public static void AddPageMessage(Page objPage, string Heading, string Message, string IconSrc)
		{
			AddPageMessage(objPage, Heading, Message, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess, IconSrc);
		}
		public static void AddPageMessage(UI.Skins.Skin objSkin, string Heading, string Message, string IconSrc)
		{
			AddPageMessage(objSkin, Heading, Message, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess, IconSrc);
		}
		public static void AddPageMessage(UI.Skins.Skin objSkin, string Heading, string Message, UI.Skins.Controls.ModuleMessage.ModuleMessageType objModuleMessageType)
		{
			AddPageMessage(objSkin, Heading, Message, objModuleMessageType, Null.NullString);
		}
		public static void AddPageMessage(Page objPage, string Heading, string Message, UI.Skins.Controls.ModuleMessage.ModuleMessageType objModuleMessageType)
		{
			AddPageMessage(objPage, Heading, Message, objModuleMessageType, Null.NullString);
		}
		public static UI.Skins.Controls.ModuleMessage GetModuleMessageControl(string Heading, string Message, string IconImage)
		{
			return GetModuleMessageControl(Heading, Message, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess, IconImage);
		}
		public static UI.Skins.Controls.ModuleMessage GetModuleMessageControl(string Heading, string Message, UI.Skins.Controls.ModuleMessage.ModuleMessageType objModuleMessageType)
		{
			return GetModuleMessageControl(Heading, Message, objModuleMessageType, Null.NullString);
		}
		public static UI.Skins.Controls.ModuleMessage GetModuleMessageControl(string Heading, string Message, UI.Skins.Controls.ModuleMessage.ModuleMessageType objModuleMessageType, string IconImage)
		{
			UI.Skins.Controls.ModuleMessage objModuleMessage;
			UI.Skins.Skin s = new UI.Skins.Skin();
			objModuleMessage = (UI.Skins.Controls.ModuleMessage)s.LoadControl("~/admin/skins/ModuleMessage.ascx");
			objModuleMessage.Heading = Heading;
			objModuleMessage.Text = Message;
			objModuleMessage.IconImage = IconImage;
			objModuleMessage.IconType = objModuleMessageType;
			return objModuleMessage;
		}
		public static UI.Skins.Skin GetParentSkin(DotNetNuke.Entities.Modules.PortalModuleBase objModule)
		{
			return GetParentSkin(objModule as System.Web.UI.Control);
		}
		public static UI.Skins.Skin GetParentSkin(System.Web.UI.Control objControl)
		{
			if (objControl != null) {
				System.Web.UI.Control MyParent = objControl.Parent;
				bool FoundSkin = false;
				while (MyParent != null) {
					if (MyParent is Skin) {
						FoundSkin = true;
						break;
					}
					MyParent = MyParent.Parent;
				}
				if (FoundSkin) {
					return (Skin)MyParent;
				} else {
					return null;
				}
			} else {
				return null;
			}
		}
		public static DotNetNuke.UI.Skins.Skin GetSkin(PageBase Page)
		{
			DotNetNuke.UI.Skins.Skin ctlSkin = null;
			string skinSource = Null.NullString;
			if ((Page.Request.QueryString["SkinSrc"] != null)) {
				skinSource = SkinController.FormatSkinSrc(Common.Globals.QueryStringDecode(Page.Request.QueryString["SkinSrc"]) + ".ascx", Page.PortalSettings);
				ctlSkin = LoadSkin(Page, skinSource);
			}
			if (ctlSkin == null) {
				if (Page.Request.Cookies["_SkinSrc" + Page.PortalSettings.PortalId.ToString()] != null) {
					if (!String.IsNullOrEmpty(Page.Request.Cookies["_SkinSrc" + Page.PortalSettings.PortalId.ToString()].Value)) {
						skinSource = SkinController.FormatSkinSrc(Page.Request.Cookies["_SkinSrc" + Page.PortalSettings.PortalId.ToString()].Value + ".ascx", Page.PortalSettings);
						ctlSkin = LoadSkin(Page, skinSource);
					}
				}
			}
			if (ctlSkin == null) {
				if (Common.Globals.IsAdminSkin())
				{
					skinSource = SkinController.FormatSkinSrc(Page.PortalSettings.DefaultAdminSkin, Page.PortalSettings);
				} else {
					skinSource = Page.PortalSettings.ActiveTab.SkinSrc;
				}
				if (!String.IsNullOrEmpty(skinSource)) {
					skinSource = SkinController.FormatSkinSrc(skinSource, Page.PortalSettings);
					ctlSkin = LoadSkin(Page, skinSource);
				}
			}
			if (ctlSkin == null) {
				skinSource = SkinController.FormatSkinSrc(SkinController.GetDefaultPortalSkin(), Page.PortalSettings);
				ctlSkin = LoadSkin(Page, skinSource);
			}
			Page.PortalSettings.ActiveTab.SkinPath = SkinController.FormatSkinPath(skinSource);
			ctlSkin.ID = "dnn";
			return ctlSkin;
		}
	}
}
